<?php

  /**
   * Profile
   *
   * Класс виджета, выводящего профиль пользователя
   */

  class Profile extends XMLWidget
  {
    /**
     * экземпляр модели пользователя
     *
     * @var User
     */
    public $user = null;

    /**
     * запустить виджет
     *
     * @return void
     * @access public
     */
    public function run()
    {
      static $fields = array("sex", "frm_birthday", "hometown", "now_live", "education", "sstatus", "mstatus", "politics", "religion");

      self::openNode(User::USER, array(
        "id"     => $this->user->id,
        "login"  => $this->user->login,
        "name"   => $this->user->name,
        "avatar" => $this->user->getAvatar()
      ));

      /** @var $user CWebUser */
      $user   = $this->controller->app->user;
      $public = $user->isGuest || ($user->id != $this->user->id) ? $this->user->publicSet : $this->user->enum("public", "set");
      self::openNode("contacts");
      array_walk($public, create_function('$f, $k, User $u', 'XMLWidget::htmlNode($f, $u->$f, array("href" => $u->getAccountUrl($f)));'), $this->user);
      self::closeNode("contacts");

      self::openNode("posts");
      $posts = Post::model();
      foreach ($posts->enum("type") as $type)
        self::node($type, $posts->$type()->postCount($this->user->id));
      self::closeNode("posts");

      self::node("about", $this->user->about, array("cdata" => true));

      self::openNode("fields");
      array_walk($fields, create_function('$f, $k, User $u', 'XMLWidget::htmlNode($f, $u->$f);'), $this->user);
      self::closeNode("fields");

      self::node("ps", $this->user->ps, array("cdata" => true));

      self::closeNode(User::USER);
    }

    /**
     * отобразить поля
     *
     * @param string $node имя узла
     * @param array  $data отображаемые данные
     * @return void
     * @access private
     */
    private function fieldNodes($node, array $data)
    {
      self::openNode($node);
      array_walk($data, create_function('$f, $k, User $u', 'XMLWidget::htmlNode($f, $u->$f);'), $this->user);
      self::closeNode($node);
    }
  }

?>
